package com.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.ao.sysrole.AddRoleAO;
import com.boot.ao.sysrole.RoleAO;
import com.boot.ao.sysrole.UpdateRoleAO;
import com.boot.common.PageVO;
import com.boot.common.po.Result;
import com.boot.entity.SysRole;
import com.boot.mapper.SysRoleMapper;
import com.boot.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements RoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;


    @Override
    public PageVO getList(RoleAO roleAO) {
        IPage<SysRole> page = new Page<>(roleAO.getPageNo(), roleAO.getPageSize());
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        //模糊匹配
        if (!StringUtils.isEmpty(roleAO.getRoleName())) {
            queryWrapper.like("role_name", roleAO.getRoleName());
        }
        IPage<SysRole> sysRoleIPage = sysRoleMapper.selectPage(page, queryWrapper);
        return PageVO.builder().pageNo(roleAO.getPageNo()).pageSize(roleAO.getPageSize()).total(sysRoleIPage.getTotal()).data(sysRoleIPage.getRecords()).build();
    }

    @Override
    public Result add(AddRoleAO addRoleAO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(addRoleAO,sysRole);
        sysRoleMapper.insert(sysRole);
       return Result.success();
    }

    @Override
    public Result delete(Long id) {
        sysRoleMapper.deleteById(id);
        return Result.success();
    }

    @Override
    public Result update(UpdateRoleAO updateRoleAO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(updateRoleAO,sysRole);
        sysRoleMapper.updateById(sysRole);
        return Result.success();
    }
}
